package com.springboot.nacos.discovery;

import com.small.nacos.api.naming.common.NamingCommonConstants;
import com.springboot.nacos.discovery.event.NacosDiscoveryInfoChangedEvent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.ApplicationEventPublisher;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Properties;


@ConfigurationProperties("small.nacos.discovery")
public class NacosDiscoveryProperties {

    @Value("${small.nacos.discovery.service:${spring.application.name:}}")
    private String service;

    private String ip;

    @Value("${small.nacos.discovery.port:${server.port:}}")
    private int port;

    private String serverAddr;

    private String namespace;

    private String group = NamingCommonConstants.DEFAULT_GROUP_NAME;

    private String clusterName = NamingCommonConstants.DEFAULT_CLUSTER_NAME;

    private boolean ephemeral = true;

    private Boolean enabled;

    /**
     * 元数据信息
     */
    private Map<String, String> metadata = new HashMap<>();


    @Autowired
    private NacosServiceManager nacosServiceManager;

    @Autowired
    private ApplicationEventPublisher applicationEventPublisher;

    @PostConstruct
    public void init() {
        applicationEventPublisher
                .publishEvent(new NacosDiscoveryInfoChangedEvent(this));
    }

    public String getService() {
        return service;
    }

    public void setService(String service) {
        this.service = service;
    }

    public String getIp() {
        return ip;
    }

    public void setIp(String ip) {
        this.ip = ip;
    }

    public int getPort() {
        return port;
    }

    public void setPort(int port) {
        this.port = port;
    }

    public String getServerAddr() {
        return serverAddr;
    }

    public void setServerAddr(String serverAddr) {
        this.serverAddr = serverAddr;
    }

    public String getNamespace() {
        return namespace;
    }

    public void setNamespace(String namespace) {
        this.namespace = namespace;
    }

    public String getGroup() {
        return group;
    }

    public void setGroup(String group) {
        this.group = group;
    }

    public String getClusterName() {
        return clusterName;
    }

    public void setClusterName(String clusterName) {
        this.clusterName = clusterName;
    }

    public Map<String, String> getMetadata() {
        return metadata;
    }

    public void setMetadata(Map<String, String> metadata) {
        this.metadata = metadata;
    }

    public boolean isEphemeral() {
        return ephemeral;
    }

    public void setEphemeral(boolean ephemeral) {
        this.ephemeral = ephemeral;
    }

    public Boolean getEnabled() {
        return enabled;
    }

    public void setEnabled(Boolean enabled) {
        this.enabled = enabled;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        NacosDiscoveryProperties that = (NacosDiscoveryProperties) o;
        return ephemeral == that.ephemeral
                && Objects.equals(serverAddr, that.serverAddr)
                && Objects.equals(namespace, that.namespace)
                && Objects.equals(group, that.group)
                && Objects.equals(metadata, that.metadata);
    }

    @Override
    public String toString() {
        return "NacosDiscoveryProperties{" +
                "serverAddr='" + serverAddr + '\'' +
                ", namespace='" + namespace + '\'' +
                ", group='" + group + '\'' +
                ", ephemeral=" + ephemeral +
                ", enabled=" + enabled +
                ", metadata=" + metadata +
                ", nacosServiceManager=" + nacosServiceManager +
                ", applicationEventPublisher=" + applicationEventPublisher +
                '}';
    }

    public Properties getNacosProperties() {
        Properties properties = new Properties();
        properties.put(NamingCommonConstants.SERVICE, Objects.toString(this.service, ""));
        properties.put(NamingCommonConstants.NAMESPACE, Objects.toString(this.namespace, ""));
        properties.put(NamingCommonConstants.SERVER_ADDR, Objects.toString(this.serverAddr, ""));
        properties.put(NamingCommonConstants.GROUP, Objects.toString(this.group, ""));

        return properties;
    }
}
